/*
 * @Author: your name
 * @Date: 2021-02-25 21:31:37
 * @LastEditTime: 2021-02-26 18:12:30
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue_test\src\router\index.js
 *
 */
import Vue from 'vue';
import VueRouter from 'vue-router';

// import Login from '@/components/Login';
// import Home from '@/components/Home';
// import Welcome from '@/components/Welcome';

const Login = () => import(/* webpackChunkName:"login_home_welcome" */ '@/components/Login.vue')
const Home = () => import(/* webpackChunkName:"login_home_welcome" */ '@/components/Home')
const Welcome = () => import(/* webpackChunkName:"login_home_welcome" */ '@/components/Welcome')


// import Users from '@/components/user/Users';
const Users = () => import(/* webpackChunkName:"user" */ '@/components/user/Users')

// import Rights from '@/components/power/Rights';
// import Roles from '@/components/power/Roles';

const Rights = () => import(/* webpackChunkName:"power" */ '@/components/power/Rights')
const Roles = () => import(/* webpackChunkName:"power" */ '@/components/power/Roles')



// import Cate from "@/components/goods/Cate";
// import Params from "@/components/goods/Params";
// import GoodList from "@/components/goods/List";
// import GoodAdd from "@/components/goods/Add";

const Cate = () => import(/* webpackChunkName:"goods" */ '@/components/goods/Cate')
const Params = () => import(/* webpackChunkName:"goods" */ '@/components/goods/Params')
const GoodList = () => import(/* webpackChunkName:"goods" */ '@/components/goods/List')
const GoodAdd = () => import(/* webpackChunkName:"goods" */ '@/components/goods/Add')

// import Order from "@/components/order/Order";
// import Report from "@/components/report/Report";

const Report = () => import(/* webpackChunkName:"report" */ '@/components/report/Report')
const Order = () => import(/* webpackChunkName:"order" */ '@/components/order/Order')


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path:'/login',
    component:Login
  },
  {
    path: '/home',
    component:Home,
    redirect: '/welcome',
    children:[
      {path:'/welcome',component:Welcome},
      {path:'/users',component:Users},
	  {path:'/rights',component:Rights},
	  {path:'/roles',component:Roles},
      {path: '/categories',component: Cate},
      {path: '/params',component: Params},
      {path: '/goods',component: GoodList},
      {path: '/goods/add',component: GoodAdd},
      {path: '/orders',component: Order},
      {path : '/reports',component: Report}
    ]
  }

]

const router = new VueRouter({
  routes
})

//挂载路由导航守卫,to表示将要访问的路径,from表示从哪里来的,next()表示下一个要做的操作
//在这里next() 表示的是放行     next('/home') 表示要强制跳转到 /home 路由
router.beforeEach((to, from, next) => {
  //如果已经登录了,就不允许去登录页面了
  const tokenStr = window.sessionStorage.getItem('token');
//如果将要访问的路径是login，就
  if(to.path == '/login'){
      next();
  }
    //获取token,token不存在的话,就跳转到login
  if(!tokenStr){
    next('/login');
  }

  if(tokenStr && to.path == '/login'){
    next(from.path);
  }
  next();
})

export default router
